#include "fig_ui_comic.h"
#include "gen_game_state.h"
#include "fighter.h"
#include "fig_vars.h"

#include "audio/audio.h"
#include "ui/ui_checkbox.h"
#include "ui/ui_manager.h"
#include "gameflow/game_flow_manager.h"
#include "gameflow/ui_popup.h"
#include "gameflow/ui_state.h"
#include "font/font_manager.h"
#include "graphics/graphics.h"
#include "graphics/graphics_util.h"
#include "application/game_application.h"

#include "util/mmgr.h"		    // Needs to come in early
#include "util/common_macros.h"		// macros
#include "util/log.h"		    // logging
#include "util/log.h"
#include "util/profiler.h"

using namespace gameflow;


FIGUIComic::FIGUIComic(camera::FbCamera*	pCamera) :
		gameflow::UIState(pCamera, "ui/comic_menu.ui"),
			m_DelayStartGame(0),
			m_IsAssetLoaded(false)
{
   
	SetupTransition("comic", "game");
	m_AlphaCounter.SetLimit(1.0f);

	m_pUILoadingTxt = GetWidget("loadingtxt");
	m_pUIComic = GetWidget("comic");
	m_pUIInfoTxt = GetWidget("info");

	if(m_pUIInfoTxt)
		m_pUIInfoTxt->SetIsVisible(false);

	if(m_pUIComic)
		m_pUIComic->SetIsActive(false);
	
	
	
}

bool		FIGUIComic::OnEnter()
{
	if(m_pUIInfoTxt)
		m_pUIInfoTxt->SetIsVisible(false);

	if(m_pUIComic)
		m_pUIComic->SetIsActive(false);

	if(m_pUILoadingTxt)
		m_pUILoadingTxt->SetIsVisible(true);


	m_DelayStartGame = 0.1f;

	/*
	int LevelNumber = ((FIGVars*)FIGVars::Instance())->GetCurrentLevel();
	if( LevelNumber < 5)
	{
		char	Buffer[256];
		sprintf(Buffer, "comic/comic_%i.jpg", LevelNumber);
		if(m_pUIComic)
			m_pUIComic->SetOffImage(Buffer);
	}
	else
	{
		if(m_pUIComic)
			m_pUIComic->SetOffImage("comic/comic_g.jpg");
	}
	*/
	FIGVars::Instance()->Save();
	//AdManager::Instance().RequestFullScreenAd(2);

	if(input::MOGAJoystick::Instance().IsConnected())
	{
		m_pUIInfoTxt->SetTextA("Press 'A' to Continue.");
	}
	else
	{
		m_pUIInfoTxt->SetTextA("LOC_TAP_ANYWHER");
	}

	return gameflow::UIState::OnEnter();
}

bool		FIGUIComic::OnExit()
{
	if(m_pUIComic)
		m_pUIComic->SetIsActive(false);

	//AdManager::Instance().Hide();

	//AdManager::Instance().RequestBannerAd();
	return gameflow::UIState::OnExit();
}

bool	FIGUIComic::Tick(float dt)
{
	m_AlphaCounter.Tick(dt);
	if(m_AlphaCounter.IsReady())
		m_AlphaCounter.Reset();
	float Counter = m_AlphaCounter.GetCounter();
	float Alpha = 0;
	if(Counter < 0.5f)
	{
		Alpha = Counter;
	}
	else
	{
		Alpha = 1.0f - Counter;
	}

	Alpha += 0.75f;
	if(Alpha > 1.0f)
		Alpha = 1.0f;

	if(m_pUIInfoTxt)
		m_pUIInfoTxt->SetTextColor(Alpha,Alpha,Alpha,1.0f);

	if(m_pUILoadingTxt)
		m_pUILoadingTxt->SetTextColor(Alpha,Alpha,Alpha,1.0f);

	if(m_DelayStartGame > 0)
	{
		m_DelayStartGame -= dt;
		if(m_DelayStartGame <= 0)
		{
			// Loading After Showing The Screen.
			if(!m_IsAssetLoaded)
			{		
				m_IsAssetLoaded = true;
				audio::BassAudio::Instance().LoadAudioSet("sounds/soundbank2.lua");
			}

			int LevelNumber = ((FIGVars*)FIGVars::Instance())->GetCurrentLevel();
			if (GENGameState* pState = GENGameState::GetGame())
			{
				pState->CachedLoad("foo");
			}
			Fighter::Instance().PlayLevel(LevelNumber);

			if(m_pUIInfoTxt)
				m_pUIInfoTxt->SetIsVisible(true);

			if(m_pUIComic)
				m_pUIComic->SetIsActive(true);

			if(m_pUILoadingTxt)
				m_pUILoadingTxt->SetIsVisible(false);
		}
	}


	return gameflow::UIState::Tick(dt);
}



void	FIGUIComic::OnMOGAKeyDownEvent(int	KeyCode)
{
	switch( (input::MOGAJoystick::KEYCODE)KeyCode)
	{
		case input::MOGAJoystick::KEYCODE_BUTTON_B:				
			gameflow::GameFlowManager::Instance().SetTransitionEffect("fade","level_select","",NULL);
		break;
		case input::MOGAJoystick::KEYCODE_BUTTON_A:				
			if(m_pUIComic && m_pUIComic->GetIsActive())
			{
				gameflow::GameFlowManager::Instance().SetTransitionEffect("fade","game","",NULL);
			}
		break;
		case input::MOGAJoystick::KEYCODE_BUTTON_START:				
			if(m_pUIComic && m_pUIComic->GetIsActive())
			{
				gameflow::GameFlowManager::Instance().SetTransitionEffect("fade","game","",NULL);
			}
		break;
		
	}
}




